package com.example.libdecodewlt;

import java.io.File;
import java.io.FileOutputStream;

public class RAW2BMP {
	static byte[] bmpHeader = new byte[] { 66, 77, -50, -105, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 102, 0, 0, 0,
			126, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	static {
		System.loadLibrary("HSRAW2BMP_hs_otg");
		System.loadLibrary("wlt2bmp_hs_otg");
	}

	public RAW2BMP() {
	}

	public static void BGR2BMP(byte[] BGRbuf, byte[] bmpBuf, String filepath) {
		for (int i = 0; i < 54; ++i) {
			bmpBuf[i] = bmpHeader[i];
		}

		int len = 38556;
		byte[] bmpRgb = new byte[len];

		int j;
		for (j = 0; j < len; j += 3) {
			bmpRgb[j] = BGRbuf[j + 2];
			bmpRgb[j + 2] = BGRbuf[j];
			bmpRgb[j + 1] = BGRbuf[j + 1];
		}

		for (j = 0; j < 126; ++j) {
			System.arraycopy(bmpRgb, j * 306, bmpBuf, 54 + j * 308, 306);
		}

		if (filepath != "") {
			bytesToImageFile(bmpBuf, filepath);
		}
	}

	private static void bytesToImageFile(byte[] bytes, String filepath) {
		try {
			File file = new File(filepath);
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(bytes, 0, bytes.length);
			fos.flush();
			fos.close();
		} catch (Exception var4) {
			var4.printStackTrace();
		}

	}

	public static native int HS_RAW2BMP(byte[] var0, byte[] var1, int var2);
}
